package com.jiaoyou.baojian.http;

import android.text.TextUtils;

import com.jiaoyou.baojian.BaojianApplication;
import com.jiaoyou.baojian.BuildConfig;
import com.jiaoyou.baojian.utils.*;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by DemonZYM on 17/5/5.
 */

public class RequestInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Request.Builder builder = chain.request().newBuilder();
        builder.addHeader("Content-Type", "text/html; charset=UTF-8")
                .addHeader("Accept-Encoding", "*")
                .addHeader("X-AJAX-CALL", "true")
                .addHeader("User-Agent", "Android/" + BuildConfig.VERSION_NAME
                        + "/" + com.jiaoyou.baojian.utils.Utils.getNetType()
                        + "/" + android.os.Build.MODEL
                        + "/" + android.os.Build.VERSION.RELEASE
                        + "/" + com.jiaoyou.baojian.utils.Utils.getChannelName())
                .addHeader("Connection", "keep-alive");
        if(!TextUtils.isEmpty(ACache.get(BaojianApplication.getInstance()).getAsString("mobile_token"))){
            builder.addHeader("X-MOBILE-TOKEN", ACache.get(BaojianApplication.getInstance()).getAsString("mobile_token"));
        }
        Request request = builder.build();
        return chain.proceed(request);
    }
}
